package cn.iocoder.yudao.module.wine.service.teamtype;

import cn.iocoder.yudao.module.wine.controller.app.teamtype.vo.AppTeamTypePageReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.wine.controller.admin.teamtype.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.teamtype.TeamTypeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.teamtype.TeamTypeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 团长缴费等级 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class TeamTypeServiceImpl implements TeamTypeService {

    @Resource
    private TeamTypeMapper teamTypeMapper;

    @Override
    public Long createTeamType(TeamTypeSaveReqVO createReqVO) {
        // 插入
        TeamTypeDO teamType = BeanUtils.toBean(createReqVO, TeamTypeDO.class);
        teamTypeMapper.insert(teamType);
        // 返回
        return teamType.getId();
    }

    @Override
    public void updateTeamType(TeamTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateTeamTypeExists(updateReqVO.getId());
        // 更新
        TeamTypeDO updateObj = BeanUtils.toBean(updateReqVO, TeamTypeDO.class);
        teamTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteTeamType(Long id) {
        // 校验存在
        validateTeamTypeExists(id);
        // 删除
        teamTypeMapper.deleteById(id);
    }

    private void validateTeamTypeExists(Long id) {
        if (teamTypeMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public TeamTypeDO getTeamType(Long id) {
        return teamTypeMapper.selectById(id);
    }

    @Override
    public PageResult<TeamTypeDO> getTeamTypePage(TeamTypePageReqVO pageReqVO) {
        return teamTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<TeamTypeDO> getTeamTypeList(AppTeamTypePageReqVO pageReqVO) {

        return teamTypeMapper.selectPTeamTypeList(pageReqVO);
    }

}
